package com.jumper.study.admin.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jumper.study.admin.dto.req.MenuReq;
import com.jumper.study.admin.dto.resp.MenuResp;
import com.jumper.study.admin.service.MenuService;
import com.jumper.study.common.api.CommonResult;
import com.jumper.study.common.api.ResultCode;
import com.jumper.study.db.model.Menu;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;



@RestController
@RequestMapping("/api/backend/menus")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("")
    public CommonResult<List<MenuResp>> getList() {
        List<MenuResp> menuList = menuService.list().stream()
                    .map(m -> new MenuResp(m.getId(), m.getTitle(), m.getUri(), m.getParentId(), m.getDesc(), m.getApis(), m.getCreatedAt()))
                    .toList();

        return CommonResult.success(menuList);
    }
    
    @GetMapping("/{id}")
    public CommonResult<MenuResp> getItem(@PathVariable Integer id) {
        Menu menu = menuService.getById(id);
        if (menu == null) {
            return CommonResult.failed(ResultCode.NOT_FOUND.getCode(), ResultCode.NOT_FOUND.getMessage());
        }
        MenuResp resp = new MenuResp();
        resp.setId(menu.getId());
        resp.setTitle(menu.getTitle());
        resp.setUri(menu.getUri());
        resp.setDesc(menu.getDesc());
        resp.setParentId(menu.getParentId());
        resp.setApis(menu.getApis());
        resp.setCreatedAt(menu.getCreatedAt());

        return CommonResult.success(resp);
    }
    
    @PostMapping("/")
    public CommonResult<Boolean> create(@RequestBody @Validated MenuReq req) {
        Menu menu = new Menu();
        menu.setId(0);
        menu.setTitle(req.getTitle());
        menu.setUri(req.getUri());
        menu.setDesc(req.getDesc());
        menu.setParentId(req.getParentId());
        menu.setApis(req.getApis());
        boolean result = menuService.save(menu);

        return CommonResult.success(result);
    }

    @PostMapping("/{id}")
    public CommonResult<Boolean> update(@PathVariable Integer id, @RequestBody @Validated MenuReq req) {
        Menu menu = menuService.getById(id);
        if (menu == null) {
            return CommonResult.failed(ResultCode.NOT_FOUND.getCode(), ResultCode.NOT_FOUND.getMessage());
        }
        menu.setTitle(req.getTitle());
        menu.setUri(req.getUri());
        menu.setDesc(req.getDesc());
        menu.setParentId(req.getParentId());
        menu.setApis(req.getApis());
        boolean result = menuService.updateById(menu);

        return CommonResult.success(result);
    }
    
    @DeleteMapping("/{id}")
    public CommonResult<Boolean> delete(@PathVariable Integer id) {
        boolean result = menuService.removeById(id);

        return CommonResult.success(result);
    }
}
